import axios from "axios";
import type { AxiosInstance, AxiosResponse } from "axios";
import GLOBAL from "./globelConfig";
import { cookie } from "./cookie";

class HttpRequest {
  constructor(baseURL: string) {
    this.instance = axios.create({
      baseURL,
      timeout: 10000, // 请求超时时间
      withCredentials: true,
    });
    let token = cookie.get("AUTHORIZATION");
    console.log("http token", token);
    // "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTY4ODU2MzAsInVzZXJfbmFtZSI6ImFkbWluIiwiYXV0aG9yaXRpZXMiOlsiUk9MRV9BRE1JTiIsIlJPTEVfVVNFUiJdLCJqdGkiOiI4Njg2NjQzZC0xNzg5LTQxZjEtYWM3Mi0wYTYzNThiOTdjZjAiLCJjbGllbnRfaWQiOiJjbGllbnQtaWQiLCJzY29wZSI6WyJhbGwiXX0._BUaIeS02rSPEYowucZM2L_HhIq4xVwGhbM36-3WT5w";
    // 请求拦截器
    this.instance.interceptors.request.use(
      (config) => {
        // 在发送请求之前做些什么，比如添加 token
        config.headers.Authorization = `Bearer ${token}`;
        config.headers.Version = GLOBAL.VERSION;
        config.headers.contentType = "application/json";
        return config;
      },
      (error: any) => {
        // 对请求错误做些什么
        return Promise.reject(error);
      }
    );

    // 响应拦截器
    this.instance.interceptors.response.use(
      (response: AxiosResponse) => {
        // 对响应数据做点什么
        return response.data;
      },
      (error: any) => {
        // 对响应错误做点什么
        return Promise.reject(error);
      }
    );
  }

  instance: AxiosInstance;
}

export default HttpRequest; // 替换为你的 API 基础 URL
